package com.zcxk.config;

import java.util.HashMap;
import java.util.Map;

public class HttpServletLocalCache {
  private static ThreadLocal<Map<String, String>> threadLocal = new ThreadLocal<>();
  private static ThreadLocal<Environment> environmentThreadLocal = new ThreadLocal<>();

  public HttpServletLocalCache() {
  }

  public static void put(String key, String value) {
    Map<String, String> stringMap = (Map) threadLocal.get();
    if (stringMap == null) {
      stringMap = new HashMap(8);
    }

    ((Map) stringMap).put(key, value);
    threadLocal.set(stringMap);
  }

  public static Environment get() {
    Environment environment = (Environment) environmentThreadLocal.get();
    return environment == null ? null : environment;
  }

  public static void put(Environment environment) {
    environmentThreadLocal.set(environment);
  }

  public static String get(String key) {
    Map<String, String> stringMap = (Map) threadLocal.get();
    return stringMap == null ? null : (String) stringMap.get(key);
  }

  public static void clearAll() {
    threadLocal.remove();
    environmentThreadLocal.remove();
  }
}
